/*
 *
 * @ProjectName:    my_umi_app
 * @Package:
 * @ClassName:      article_api
 * @Description:    文章相关接口
 * @Author:         SuPeiSen
 * @CreateDate:     2021/1/29 4:21 下午
 * @UpdateDate:     2021/1/29 4:21 下午
 * @UpdateRemark:   更新说明：
 * @Version:        1.0
 */

import { AxiosParam, Requests } from '@/utils/requests';
import { ApiPath } from '@/config/api_config';
import { ArticleListItemType, ArticleListType } from '@/type/article_type';

export const AddArticleApi = async (
  param: any,
  finallyCallBack: () => void,
) => {
  const model = await Requests.defaults.http('post', ApiPath.addArticle, {
    postData: param,
    callBack: {
      finally: finallyCallBack,
    },
  });
  return model.success;
};

// 后台文章列表
export const ArticleListByAdminApi = async (param: any) => {
  return Requests.defaults.http<ArticleListType>(
    'post',
    ApiPath.articleListByAdmin,
    {
      postData: param,
    },
  );
};

// 后台删除文章
export const DeleteArticleByAdminApi = async (id: number) => {
  const model = await Requests.defaults.http(
    'get',
    ApiPath.deleteArticleByAdmin + `/${id}`,
  );
  return model.success;
};

// 获取文章详情
export const GetArticleDetailsByAdminApi = async (id: number) => {
  const model = await Requests.defaults.http<ArticleListItemType>(
    'get',
    ApiPath.getArticleByAdmin + `/${id}`,
  );
  return model;
};

// 更新文章
export const UpdateArticleDetailsByAdminApi = async (
  param: any,
  finallyCallBack: () => void,
) => {
  const model = await Requests.defaults.http<ArticleListItemType>(
    'put',
    ApiPath.updateArticleByAdmin,
    {
      postData: param,
      callBack: {
        finally: finallyCallBack,
      },
    },
  );
  return model.success;
};

// 更新文章状态
export const updateArticleStatusApi = async (param: {
  id: number;
  status: boolean;
}) => {
  const statusNum = param.status ? 1 : 0;
  const model = await Requests.defaults.http(
    'put',
    ApiPath.updateArticleStatus,
    {
      postData: {
        id: param.id,
        status: statusNum,
      },
    },
  );
  return model.success;
};
